﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Windows.Forms;

namespace PrVyd.Zakladni_tridy
{
    class ConfigManager
    {
        private static readonly string CONFIG_PATH = "config.xml";

        private static XDocument Config
        {
            set
            {
                if (value == null)
                    return;

                try
                {
                    value.Save(CONFIG_PATH);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Config.xml se nepodařilo uložit. " + e.Message);
                }
            }
            get
            {
                return File.Exists(CONFIG_PATH) ? XDocument.Load(CONFIG_PATH) : CreateConfig();
            }

        }

        public static string Name
        {
            get
            {
                return Config.Root.Element("name") == null ? "" : Config.Root.Element("name").Value;
            }
            set
            {
                SaveElement("name", value);
            }
        }

        public static string Surname
        {
            get
            {
                return Config.Root.Element("surname") == null ? "" : Config.Root.Element("surname").Value;
            }
            set
            {
                SaveElement("surname", value);
            }
        }

        public static string Street
        {
            get
            {
                return Config.Root.Element("street") == null ? "" : Config.Root.Element("street").Value;
            }
            set
            {
                SaveElement("street", value);
            }
        }

        public static string City
        {
            get
            {
                return Config.Root.Element("city") == null ? "" : Config.Root.Element("city").Value;
            }
            set
            {
                SaveElement("city", value);
            }
        }

        public static string Zip
        {
            get
            {
                return Config.Root.Element("zip") == null ? "" : Config.Root.Element("zip").Value;
            }
            set
            {
                SaveElement("zip", value);
            }
        }

        public static string Bill
        {
            get
            {
                return Config.Root.Element("bill") == null ? "" : Config.Root.Element("bill").Value;
            }
            set
            {
                SaveElement("bill", value);
            }
        }

        public static string BankCode
        {
            get
            {
                return Config.Root.Element("bankcode") == null ? "" : Config.Root.Element("bankcode").Value;
            }
            set
            {
                SaveElement("bankcode", value);
            }
        }

        public static string ICO
        {
            get
            {
                return Config.Root.Element("ico") == null ? "" : Config.Root.Element("ico").Value;
            }
            set
            {
                SaveElement("ico", value);
            }
        }

        public static string DIC
        {
            get
            {
                return Config.Root.Element("dic") == null ? "" : Config.Root.Element("dic").Value;
            }
            set
            {
                SaveElement("dic", value);
            }
        }

        private static void SaveElement(string name, object value)
        {
            XDocument doc = Config;
            XElement el = doc.Root.Element(name);

            el.Remove();
            doc.Root.Add(new XElement(name, value));

            Config = doc;
        }

        private static XDocument CreateConfig()
        {
            XDocument doc = new XDocument();
            XElement root = new XElement("config");
            root.SetAttributeValue("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            root.SetAttributeValue("xsi:noNamespaceSchemaLocation", "config.xsd");

            root.Add(new XElement("name"));
            root.Add(new XElement("surname"));
            root.Add(new XElement("street"));
            root.Add(new XElement("city"));
            root.Add(new XElement("zip"));
            root.Add(new XElement("bill"));
            root.Add(new XElement("bankcode"));
            root.Add(new XElement("ico"));
            root.Add(new XElement("dic"));

            doc.Add(root);

            return doc;
        }
    
    }
}
